// pages/ucenter/personcenter/editdetails/circleinteraction/foundationsetup/punchtime/punchtime.js
const util = require('../../../../../../../../utils/util.js');
const api = require('../../../../../../../../config/api.js');
const user = require('../../../../../../../../services/user.js');
const app = getApp();
Page({
  data: {
    circleid: '',
    //打卡时间类型
    dayType: 0,
    //活动时间类型
    periodType: 0,
    //打卡开始时间
    startTime: '00:00',
    //打卡结束时间最低选择
    minStartTime: '',
    //打卡结束时间
    endTime: '23:59',
    //提醒开关
    remind: false,
    //添加提醒时间的控制
    remindPanel: false,
    //提醒的时间
    remindTime: "06:00",
    //提醒周期
    dayList: [false, false, false, false, false, false, false, false],
    //保存开关
    openSave: false,
  },
  onLoad: function (options) {
    var _this = this;
    var circleid = options.circleid;
    _this.setData({
      circleid: circleid
    })
    console.log(circleid)
  },
  onShow: function () {
    var _this = this
    _this.checkTime();
    var date = new Date();
    var newDate = util.getDateStr(date, 0);
    var tomorrow = util.getDateStr(date, 1);
    _this.setData({
      newDate: newDate,
      startDate: newDate,
      minEndDate: tomorrow,
      endDate: tomorrow
    })
  },
  //选择每日打卡时间类型
  selectDayType: function (e) {
    var _this = this
    var selId = e.currentTarget.dataset.id
    _this.setData({
      dayType: selId
    })
    _this.checkTime();
  },
  //选择活动日期类型
  selectPeriodType: function (e) {
    var _this = this
    var selId = e.currentTarget.dataset.id
    _this.setData({
      periodType: selId
    })
    _this.checkTime();
  },
  //打卡开始时间选择
  startTimeChange: function (e) {
    var _this = this
    var value = e.detail.value
    _this.setData({
      startTime: value
    })
    _this.checkTime();
  },
  //打卡结束时间选择
  endTimeChange: function (e) {
    var _this = this
    var value = e.detail.value
    _this.setData({
      endTime: e.detail.value
    })
    _this.checkTime();
  },
  //选择活动开始时间
  startDateChange: function (e) {
    var _this = this
    var date = e.detail.value
    var end = _this.data.endDate
    var minEndDate = util.getDateStr(date, 1);
    _this.setData({
      startDate: date,
      minEndDate: minEndDate
    })
    if (new Date(date).getTime() > new Date(end).getTime()) {
      _this.setData({
        endDate: minEndDate
      })
    } else if (new Date(date).getTime() == new Date(end).getTime()) {
      _this.setData({
        endDate: minEndDate
      })
    }
  },
  //选择活动结束时间
  endDateChange: function (e) {
    var _this = this
    var date = e.detail.value
    _this.setData({
      endDate: date
    })
  },
  //打卡提醒开关
  checkRemind: function (e) {
    var _this = this
    _this.setData({
      remind: e.detail.value
    })
  },
  //打开提醒时间面板
  openRemindMC: function () {
    var _this = this
    _this.setData({
      remindPanel: true
    })
  },
  //关闭提醒时间面板
  closeMC: function () {
    var _this = this
    _this.setData({
      remindPanel: false
    })
  },
  //选择提醒时间
  remindTimeChange: function (e) {
    var _this = this
    var date = e.detail.value
    _this.setData({
      remindTime: date
    })
  },
  //选择提醒周期
  selectRemindDay: function (e) {
    var _this = this
    var sel = e.currentTarget.dataset.index
    var arr = _this.data.dayList
    if (sel == 7) {
      if (arr[sel]) {
        for (var i = 0; i <= arr.length - 1; i++) {
          arr[i] = false
        }
        _this.setData({
          dayList: arr
        })
      } else {
        for (var i = 0; i <= arr.length - 1; i++) {
          arr[i] = true
        }
        _this.setData({
          dayList: arr
        })
      }
    } else {
      if (arr[sel]) {
        arr[sel] = false
      } else {
        arr[sel] = true
      }
      for (var i = 0; i < arr.length; i++) {
        if (arr[i] == false) {
          arr[7] = false
        }
      }
      _this.setData({
        dayList: arr
      })
    }
  },
  //效验时间
  checkTime: function () {
    var _this = this
    if (_this.data.dayType == 1) {
      var start = _this.data.startTime.replace(/:/g, '') * 1
      var end = _this.data.endTime.replace(/:/g, '') * 1
      console.log(start)
      console.log(end)
      if (end < start) {
        console.log(111111)
        wx.showToast({
          title: '打卡时间无效',
          icon: 'none',
          duration: 1500
        })
        _this.setData({
          openSave: false
        })
      } else if (end == start) {
        console.log(222222)
        wx.showToast({
          title: '打卡时间无效',
          icon: 'none',
          duration: 1500
        })
        _this.setData({
          openSave: false
        })
      }
    } else {
      _this.setData({
        openSave: true
      })
    }
  }
})